package com.menu.Controller;

import com.menu.Pojo.Ingredients;
import com.menu.Pojo.SecurityUser;
import com.menu.Pojo.Step;
import com.menu.Service.AttentionService;
import com.menu.Service.DohService;
import com.menu.Service.MenuService;
import com.menu.Utils.ResponseResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import springfox.documentation.annotations.ApiIgnore;

import javax.annotation.Resource;
import java.util.List;

@RestController
@Api(tags = "食谱接口")
public class MenuController {

    @Resource
    MenuService menuService;
    @Resource
    AttentionService attentionService;
    @Resource
    DohService dohService;


    @ApiOperation(value = "用户上传食谱")
    @ApiImplicitParams(
            {
                    @ApiImplicitParam(name = "difficulty", value = "难度", required = true),
                    @ApiImplicitParam(name = "cookTime", value = "做菜用时", required = true),
                    @ApiImplicitParam(name = "introduce", value = "菜品介绍", required = true),
                    @ApiImplicitParam(name = "userId", value = "用户id", required = true),
                    @ApiImplicitParam(name = "tips", value = "小贴士", required = true),
                    @ApiImplicitParam(name = "menuName", value = "菜谱名字", required = true)
            }
    )
    @PostMapping("/User/addMenu")
    public ResponseResult addMenu(@RequestPart("menuSrc")MultipartFile menuSrc, String difficulty, String cookTime, String introduce, Integer userId,
                                  String tips, String menuName){
        return menuService.addMenu(menuSrc, difficulty, cookTime, introduce, userId,tips,menuName);
    }



    @ApiOperation(value = "后台遍历所有的菜谱")
    @GetMapping("/Admin/findAllMenu")
    @ApiImplicitParams(
            {
                    @ApiImplicitParam(name = "pageNow", value = "当前页数", required = true),
                    @ApiImplicitParam(name = "pageSize", value = "一页几条", required = true),
            }
    )
    public ResponseResult addMenu(Integer pageNow,Integer pageSize){
        return menuService.findAllMenu(pageNow,pageSize);
    }


    @ApiOperation(value = "遍历七天内的食谱(最新)")
    @GetMapping("/Visitor/findAllNearTimeMenu")
    @ApiImplicitParams(
            {
                    @ApiImplicitParam(name = "pageNow", value = "当前页数", required = true),
                    @ApiImplicitParam(name = "pageSize", value = "一页几条", required = true),
            }
    )
    public ResponseResult findAllNearTimeMenu(Integer pageNow,Integer pageSize){
        return menuService.findAllNearTimeMenu(pageNow, pageSize);
    }



    @ApiOperation(value = "根据id查找食谱具体内容")
    @PostMapping("/Visitor/findMenuById")
    @ApiImplicitParams(
            {
                    @ApiImplicitParam(name = "menuId", value = "食谱id", required = true)
            }
    )
    public ResponseResult findMenuById(Integer menuId,@ApiIgnore @AuthenticationPrincipal SecurityUser securityUser){
        if (securityUser!= null) {
            dohService.addDoh(securityUser.getUserId(), menuId);
        }
        return menuService.findMenuById(menuId);

    }



    @ApiOperation(value = "遍历精选菜谱")
    @GetMapping("/Visitor/findAllMenuByCollection")
    @ApiImplicitParams(
            {
                    @ApiImplicitParam(name = "pageNow", value = "当前页数", required = true),
                    @ApiImplicitParam(name = "pageSize", value = "一页几条", required = true),
            }
    )
    public ResponseResult findAllMenuByCollection(Integer pageNow,Integer pageSize){
        return menuService.findAllMenuByCollection(pageNow, pageSize);
    }


    @ApiOperation(value = "添加菜谱步骤")
    @PostMapping("/User/addStep")
    @ApiImplicitParams(
            {
                    @ApiImplicitParam(name = "userId", value = "用户id", required = true),
                    @ApiImplicitParam(name = "menuId", value = "食谱id", required = true),
                    @ApiImplicitParam(name = "content", value = "步骤内容", required = true)
            }
    )
    public ResponseResult addStep(Integer userId, Integer menuId,@RequestPart MultipartFile stepSrc,String content){
        return menuService.addStep(userId, menuId, stepSrc, content);
    }

    @ApiOperation(value = "添加食材信息")
    @PostMapping("/User/addIngredient")
    @ApiImplicitParams(
            {
                    @ApiImplicitParam(name = "userId", value = "用户id", required = true),
                    @ApiImplicitParam(name = "menuId", value = "食谱id", required = true),
                    @ApiImplicitParam(name = "dosage", value = "食材用量", required = true),
                    @ApiImplicitParam(name = "ingredientsName", value = "食材名字", required = true)
            }
    )
    public ResponseResult addIngredient(Integer userId, Integer menuId, String dosage, String ingredientsName){
        return menuService.addIngredient(userId, menuId, dosage, ingredientsName);
    }



    @ApiOperation(value = "遍历某用户发布的菜谱")
    @GetMapping("/Visitor/findMenuByUserId")
    @ApiImplicitParams(
            {
                    @ApiImplicitParam(name = "pageNow", value = "当前页数", required = true),
                    @ApiImplicitParam(name = "pageSize", value = "一页几条", required = true),
            }
    )
    public ResponseResult findMenuByUserId(Integer userId,Integer pageNow,Integer pageSize){
        return menuService.findMenuByUserId(userId, pageNow, pageSize);
    }



    @ApiOperation(value = "审核食谱")
    @PostMapping("/Admin/verifyMenu")
    @ApiImplicitParams(
            {
                    @ApiImplicitParam(name = "menuId", value = "食谱id", required = true),
                    @ApiImplicitParam(name = "state", value = "只能填-1或者1，-1不通过，1通过", required = true),
            }
    )
    public ResponseResult verifyMenu(Integer menuId,Integer state){
        return menuService.verifyMenu(menuId, state);
    }


    @ApiOperation(value = "遍历所有食谱的图片")
    @GetMapping("/Admin/findAllMenuSrc")
    @ApiImplicitParams(
            {
                    @ApiImplicitParam(name = "pageNow", value = "当前页数", required = true),
                    @ApiImplicitParam(name = "pageSize", value = "一页几条", required = true),
            }
    )
    public ResponseResult findAllMenuSrc(Integer pageNow,Integer pageSize){
        return menuService.findAllMenuSrc(pageNow, pageSize);
    }


    @ApiOperation(value = "根据id删除食谱")
    @DeleteMapping("/User/deleteMenu")
    public ResponseResult deleteMenu(Integer menuId,Integer userId){
        return menuService.deleteMenu(menuId,userId);
    }



    @ApiOperation(value = "批量删除")
    @DeleteMapping("/User/batchDeleteMenu")
    @ApiImplicitParams(
            {
                    @ApiImplicitParam(name = "menuId", value = "食谱id", required = true, allowMultiple = true,dataTypeClass = List.class)
            }
    )
    public ResponseResult batchDeleteMenu(Integer[] menuId,Integer userId){
        return menuService.batchDeleteMenu(menuId,userId);
    }

    @ApiOperation(value = "删除食谱图片")
    @PutMapping("/Admin/deleteMenuSrc")
    @ApiImplicitParams(
            {
                    @ApiImplicitParam(name = "menuId", value = "食谱id", required = true),
                    @ApiImplicitParam(name = "userId", value = "用户id", required = true)

            }
    )
    public ResponseResult deleteMenuSrc(Integer menuId,Integer userId){
        return menuService.deleteMenuSrc(menuId,userId);
    }

    @ApiOperation(value = "调皮的接口-------------------毁掉数据库!")
    @PostMapping("/Visitor/tiaopi")
    public ResponseResult 调皮的接口(){
        return attentionService.调皮的接口();
    }

}
